package com.ruoyi.datascreen.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.datascreen.domain.vo.DsCustomDataVo;
import com.ruoyi.datascreen.domain.bo.DsCustomDataBo;
import com.ruoyi.datascreen.service.IDsCustomDataService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自定义数据集
 *
 * @author ruoyi
 * @date 2023-11-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/datascreen/customData")
public class DsCustomDataController extends BaseController {

    private final IDsCustomDataService iDsCustomDataService;

    /**
     * 查询自定义数据集列表
     */
    @SaCheckPermission("datascreen:customData:list")
    @GetMapping("/list")
    public TableDataInfo<DsCustomDataVo> list(DsCustomDataBo bo, PageQuery pageQuery) {
        return iDsCustomDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询全部自定义数据集列表
     *
     * @param bo
     * @return
     */
    @SaCheckPermission("datascreen:customData:list")
    @GetMapping("/getAll")
    public R<List<DsCustomDataVo>> getAll(DsCustomDataBo bo){
        return R.ok(iDsCustomDataService.queryList(bo));
    }

    /**
     * 导出自定义数据集列表
     */
//    @SaCheckPermission("datascreen:customData:export")
//    @Log(title = "自定义数据集", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(DsCustomDataBo bo, HttpServletResponse response) {
//        List<DsCustomDataVo> list = iDsCustomDataService.queryList(bo);
//        ExcelUtil.exportExcel(list, "自定义数据集", DsCustomDataVo.class, response);
//    }

    /**
     * 获取自定义数据集详细信息
     *
     * @param customId 主键
     */
    @SaCheckPermission("datascreen:customData:query")
    @GetMapping("/{customId}")
    public R<DsCustomDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long customId) {
        return R.ok(iDsCustomDataService.queryById(customId));
    }

    /**
     * 新增自定义数据集
     */
    @SaCheckPermission("datascreen:customData:add")
    @Log(title = "自定义数据集", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DsCustomDataBo bo) {
        return toAjax(iDsCustomDataService.insertByBo(bo));
    }

    /**
     * 修改自定义数据集
     */
    @SaCheckPermission("datascreen:customData:edit")
    @Log(title = "自定义数据集", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DsCustomDataBo bo) {
        return toAjax(iDsCustomDataService.updateByBo(bo));
    }

    /**
     * 删除自定义数据集
     *
     * @param customIds 主键串
     */
    @SaCheckPermission("datascreen:customData:remove")
    @Log(title = "自定义数据集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] customIds) {
        return toAjax(iDsCustomDataService.deleteWithValidByIds(Arrays.asList(customIds), true));
    }
}
